#include <allegro.h>
#include <stdio.h>

/* handler du chrono */
volatile unsigned long timer;
void chrono()
{
  timer++;
}
END_OF_FUNCTION(chrono);

/* fonction main... */
int main()
{
  allegro_init();
  int i;

  install_timer();
  install_keyboard();

  set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 320, 240, 0, 0);
  set_color_depth(24);

  LOCK_VARIABLE(timer);
  LOCK_FUNCTION(chrono);

  install_int(chrono, 10);

  PALETTE pal;

  BITMAP *buffer = create_bitmap(320, 240);
  BITMAP *chrono = load_bmp("timer.bmp", pal);
  BITMAP *number[10]; /* les nombres */
  BITMAP *dp = create_bitmap(8, 16); /* les deux-points */

  for (i=0; i<10; i++)
  {
    number[i] = create_bitmap(8, 16);
    blit(chrono, number[i], i*8, 0, 0, 0, 8, 16);
  }
  blit(chrono, dp, 80, 0, 0, 0, 8, 16);

  char min, sec, cen; /* minutes secondes centièmes */
  int x=0;
  while(! key[KEY_ESC])
  {
    poll_keyboard();
    clear_to_color(buffer, makecol(0, 0, 255));

    if(key[KEY_SPACE]) timer = 0;
    cen = timer % 100;
    sec = (timer / 100) % 60;
    min = (timer / 6000) % 60;

    x=132;
    masked_blit(number[min/10], buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(number[min%10], buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(dp, buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(number[sec/10], buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(number[sec%10], buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(dp, buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(number[cen/10], buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;
    masked_blit(number[cen%10], buffer, 0, 0, x, SCREEN_H/3, 8, 16); x += 8;

    blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  }

  allegro_exit();
  return 0;
}

END_OF_MAIN();
